package com.yz.jdd.base.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

/**
 * Created by shibin on 2016/12/29.
 */

public class IntentHandler {
    /**
     * 打开登录界面
     *
     * @param context
     * @return
     */
    public static boolean startLoginActivity(Context context) {
        return startNativeActivityByComponentName(context,
                "com.yz.jdd.module.login.view.LoginActivity", null);
    }


    /**
     * 会员信息界面打开支付宝绑卡页面
     *
     * @param context
     * @param memberCardId
     * @return
     */
    public static boolean startCardAlipayBindActivity(Context context, String memberCardId) {
        Bundle args = null;
        if (!TextUtils.isEmpty(memberCardId)) {
            args = new Bundle();
            args.putString("intent_membercardid", memberCardId);
        }

        return startNativeActivityByComponentName(context,
                "com.yunzong.members.relate.CardAlipayBindActivity", args);
    }


    /**
     * h5页面跳转到支付密码设置界面
     *
     * @param context
     * @return
     */

    public static boolean startCardPhoneModifyPasswordActivity(Context context, String memberCardId, String phoneNum) {
        Bundle args = null;
        if (!TextUtils.isEmpty(memberCardId)) {
            args = new Bundle();
            args.putString("intent_membercardid", memberCardId);
            args.putString("intent_phonenum", phoneNum);
        }

        return startNativeActivityByComponentName(context,
                "com.yunzong.members.relate.CardPhoneModifyPasswordActivity", args);
    }

    public static boolean startCashierActivity(Context context) {
        return startNativeActivityByComponentName(context,
                "com.yunzong.pay.cashier.CashierActivity", null);
    }


    /**
     * 跳到会员验证
     *
     * @param context
     * @return
     */

    public static boolean startMemberVerifyAndQueryActivity(Context context,
                                                            String type, String from) {
        Bundle args = null;
        if (!TextUtils.isEmpty(type)) {
            args = new Bundle();
            args.putString("verify_or_query", type);
            args.putString("intent_from", from);
        }
        return startNativeActivityByComponentName(context,
                "com.yunzong.members.card.verifyandquery.CardVerifyAndQueryActivity", args);
    }

    public static boolean startCardShipActivity(Context context) {
        return startNativeActivityByComponentName(context,
                "com.yunzong.members.CardShipCardActivity", null);
    }

    public static boolean startCardPrePaidActivity(Context context, Bundle args) {
        return startNativeActivityByComponentName(context,
                "com.yunzong.members.card.prepaied.CardPrePaidActivity", args);
    }

    /**
     * 跳到退款
     *
     * @param context
     * @param orderId       订单号
     * @param tradeNo       交易流水号
     * @param memeberCardId 会员卡号
     * @param orderAmount   应收金额
     * @param payType       支付方式
     *                      //* @param orderType     订单类型：1 交易订单 2 储值订单
     * @return
     */
    public static boolean startRefundActivity(Context context, String orderId, String tradeNo, String memeberCardId, String orderAmount, String payType) {
        Bundle args = null;
        if (!TextUtils.isEmpty(orderId)) {
            args = new Bundle();
            args.putString("intent_orderid", orderId);
            args.putString("intent_membercardid", memeberCardId);
            args.putString("intent_orderamount", orderAmount);
            args.putString("intent_paytype", payType);

        }
        return startNativeActivityByComponentName(context, "com.yunzong.pay.cashier.refund.RefundActivity", args);
    }

    /**
     * 跳转到选择支付方式
     *
     * @param context
     * @param args    参数
     * @return
     */
    public static boolean startPaytypeActivity(Context context, Bundle args) {
        return startNativeActivityByComponentName(context, "com.yunzong.pay.cashier.PaytypeActivity", args);
    }

    public static boolean startAlipayBindActivity(Context context) {
        return startNativeActivityByComponentName(context, "com.yunzong.members.relate.CardAlipayBindActivity", null);
    }

    /**
     * 跳转到指定activity
     *
     * @param context
     * @param componentName activity包名
     * @param args
     * @return
     */
    public static boolean startNativeActivityByComponentName(Context context,
                                                             String componentName, Bundle args) {
        Intent intent = sIntent;
        if (intent == null) {
            intent = new Intent();
        }
        setIntentOnce(null);

        try {
            @SuppressWarnings("rawtypes")
            Class claa = Class.forName(componentName);
            intent.setClass(context, claa);
        } catch (Exception e) {
            return false;
        }

        // ComponentName cn = new ComponentName(context, componentName);
        // intent.setComponent(cn);
        if (!isActivityExists(context, intent)) {
            return false;
        }
        if (args != null) {
            intent.putExtras(args);
        }
        if (context instanceof Activity) {
            ((Activity) context).startActivityForResult(intent, 0);
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
        return true;
    }

    /**
     * 验证是否可达
     *
     * @param context
     * @param intent
     * @return
     */
    public static boolean isActivityExists(Context context, Intent intent) {
        if (context.getPackageManager().resolveActivity(intent, 0) != null) {
            return true;
        }
        return false;
    }

    private static Intent sIntent;

    public static void setIntentOnce(Intent intent) {
        sIntent = intent;
    }
}
